package com.lx.huimail.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lx.huimail.R;
import com.lx.huimail.configs.Config;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;


public class SafeSetActivity extends Activity {
    private RelativeLayout relativeLayout;
    private TextView logout;
    private RelativeLayout fanhui;
    public static Activity instance;
    private CheckBox auto_login;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_safe_set);
        instance=this;
        relativeLayout=(RelativeLayout)findViewById(R.id.lly_pwd);
        logout=(TextView)findViewById(R.id.tv_logout);
        fanhui=(RelativeLayout)findViewById(R.id.fanhui);
        fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SafeSetActivity.this.finish();
            }
        });
        relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(SafeSetActivity.this,ForgetPasswordActivity.class);
                intent.putExtra("hello","1");
                startActivity(intent);

            }
        });
        logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(SafeSetActivity.this)
                        .setTitle("退出")
                        .setMessage("确定退出登陆吗")
                        .setPositiveButton("确定",
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog,int which) {
                                        String url= Config.url+"login/outLogin.do";

                                        OkHttpUtils
                                                .post()
                                                .url(url)
                                                .build()
                                                .execute(new StringCallback() {
                                                    @Override
                                                    public void onError(Call call, Exception e, int id) {

                                                        Toast.makeText(SafeSetActivity.this,"连接服务器失败",Toast.LENGTH_SHORT).show();
                                                    }
                                                    @Override
                                                    public void onResponse(String response, int id) {
                                                        if(response.equals("true")){
                                                            SharedPreferences preferences=getSharedPreferences("user", Context.MODE_PRIVATE);
                                                            SharedPreferences.Editor editor=preferences.edit();
                                                            editor.putBoolean("status",false);
                                                            editor.putString("out","1");
                                                            editor.commit();
                                                            Toast.makeText(SafeSetActivity.this,"退出登录成功",Toast.LENGTH_SHORT).show();
                                                            SafeSetActivity.this.sendBroadcast(new Intent(Config.INTENT_ACTION_LOGOUT));
                                                            if(LoginActivity.instance!=null){
                                                                auto_login=(CheckBox)LoginActivity.instance.findViewById(R.id.cb_auto);
                                                                auto_login.setChecked(false);
                                                                LoginActivity.instance.finish();

                                                            }

                                                            startActivity(new Intent(SafeSetActivity.this,LoginActivity.class));
                                                            SharedPreferences sp=getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE);
                                                            sp.edit().putBoolean("ISCHECK", false).commit();
                                                            sp.edit().putBoolean("AUTO_ISCHECK", false).commit();
                                                            finish();
                                                            if (MainFragmentActivity.instance!=null){
                                                                MainFragmentActivity.instance.finish();
                                                            }

                                                        }else {
                                                            Toast.makeText(SafeSetActivity.this,"退出登录失败，请重新退出",Toast.LENGTH_SHORT).show();
                                                        }

                                                    }
                                                });
                                    }
                                })
                        .setNegativeButton("取消",new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,int which) {
                            }
                        }).setCancelable(false).show();

            }
        });
    }
}
